//
//  YYLHomeOriginalView.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/3/31.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit
import SnapKit

class YYLHomeOriginalView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
        backgroundColor = UIColor.white
        addSubview(iconImage)
        addSubview(screen_nameLabel)
        addSubview(created_atLabel)
        addSubview(sourceLabel)
        addSubview(textLabel)
        addSubview(mbrankImage)
        addSubview(verified_typeImage)
        addSubview(pictureView)
        pictureView.backgroundColor = self.backgroundColor
        //头像布局
        iconImage.snp.makeConstraints { (make) in
            make.left.top.equalTo(self).offset(homeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        //认证布局
        verified_typeImage.snp.makeConstraints { (make) in
            make.right.equalTo(iconImage).offset(5)
            make.bottom.equalTo(iconImage).offset(5)
        }
        //名字布局
        screen_nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImage.snp.right).offset(homeCellMargin)
            make.top.equalTo(iconImage)
        }
        //会员等级布局
        mbrankImage.snp.makeConstraints { (make) in
            make.left.equalTo(screen_nameLabel.snp.right).offset(homeCellMargin)
            make.top.centerY.equalTo(screen_nameLabel)
        }
        //发布时间布局
        created_atLabel.snp.makeConstraints { (make) in
            make.left.equalTo(screen_nameLabel)
            make.bottom.equalTo(iconImage.snp.bottom)
        }
        //来源布局
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(created_atLabel.snp.right).offset(homeCellMargin)
            make.bottom.equalTo(created_atLabel)
        }
        //微博内容
        textLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImage)
            make.top.equalTo(iconImage.snp.bottom).offset(homeCellMargin)
            make.right.equalTo(self).offset(-homeCellMargin)
        }
        //图片组布局
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(iconImage)
            make.top.equalTo(textLabel.snp.bottom).offset(homeCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
            
        }
        //self的布局
        self.snp.makeConstraints { (make) in
            make.bottom.equalTo(pictureView.snp.bottom).offset(homeCellMargin)
        }
        
        
    }
    //设置各个控件的内容
    var originalVM:YYLOriginalViewModel? {
        didSet{
            
            let iconUrl = URL(string: (originalVM?.homeModel?.user?.profile_image_url)!)
            iconImage.sd_setImage(with: iconUrl)
            screen_nameLabel.text = originalVM?.homeModel?.user?.screen_name
            textLabel.text = originalVM?.homeModel?.text
            mbrankImage.image = originalVM?.mbrankImage
            verified_typeImage.image = originalVM?.verified_typeImage
            
            sourceLabel.text = originalVM?.sourceContent
            created_atLabel.text = originalVM?.timeContent
        
            //清除约束
            viewBottomConstraint?.deactivate()
            
            if let pic_urls = originalVM?.homeModel?.pic_urls, pic_urls.count > 0 {
            //原创微博有图片
                self.snp.makeConstraints { (make) in
                  viewBottomConstraint = make.bottom.equalTo(pictureView.snp.bottom).offset(homeCellMargin).constraint
                }
                
                //给图片赋值
                pictureView.picUrls = pic_urls
            
            } else {
            //原创微博没有图片
                self.snp.makeConstraints { (make) in
                    viewBottomConstraint = make.bottom.equalTo(textLabel.snp.bottom).offset(homeCellMargin).constraint
                }
            
            }
            
        }
    
    }
    
    //头像图片
    lazy var iconImage: UIImageView = {
    let img = UIImageView(image: UIImage(named: "avatar_default_big"))
        img.layer.cornerRadius = 17
        img.layer.masksToBounds = true
    return img
    }()

    //名字
    lazy var screen_nameLabel: UILabel = UILabel(text: "新浪新浪", font: 14, color: UIColor.darkGray)
    //会员等级
    lazy var mbrankImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //认证类型
    lazy var verified_typeImage: UIImageView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
    
    //微博发布时间
    lazy var created_atLabel: UILabel = UILabel(text: "刚刚", font: 12, color: UIColor.orange)
    
    //微博来自
    lazy var sourceLabel: UILabel = UILabel(text: "来自:微博", font: 12, color: UIColor.lightGray)
    
    //微博内容
    lazy var textLabel: UILabel = {
        let label = UILabel(text: "内容发的撒范德萨手动内容发的撒范德萨手动内容发的撒范德萨手动内容发的撒范德萨手动内容发的撒范德萨手动内容发的撒范德萨手动内容发的撒范德萨手动内容发的撒范德萨手动", font: 13, color: UIColor.gray)
        label.numberOfLines = 0
        
        return label
    }()
    
    //懒加载控价 图片数组视图
    lazy var pictureView:YYLHomePictureView = YYLHomePictureView()
    
    //设置底部约束绑定
    fileprivate var viewBottomConstraint: Constraint?
}
